package net.didactylos.helpline.action;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.mail.MessagingException;

import org.hibernate.Session;

import net.didactylos.helpline.data.*;
import net.didactylos.helpline.data.Message;
import net.didactylos.helpline.servlet.EmailManager;
import net.didactylos.helpline.servlet.EmailRejectAction;
import net.didactylos.helpline.stripes.bean.HelplineActionBean;
import net.didactylos.helpline.stripes.security.Capabilities;
import net.didactylos.helpline.stripes.security.Secured;
import net.sourceforge.stripes.action.*;

public class RejectEmailAction extends HelplineActionBean
{
	private EmailRejectAction rejectAction;
	
	private Message message;

	public EmailRejectAction getRejectAction()
	{
		return rejectAction;
	}

	public void setRejectAction(EmailRejectAction rejectAction)
	{
		this.rejectAction = rejectAction;
	}

	public Message getMessage()
	{
		return message;
	}

	public void setMessage(Message message)
	{
		this.message = message;
	}

	@Secured(Capabilities.SIGN_IN)
	public Resolution rejectEmail()
	{
		try
		{
			Conversation conv = message.getConversation();
			
			switch (rejectAction)
			{
			case SPAM:
				EmailManager.moveToSpam(message);
				getContext().getMessages().add(new LocalizableMessage("rejectSpamMessage"));
				message.setStatus(MessageStatus.REJECTED);
				conv.setClosed(true);
				break;
			case INAPPROPRIATE:
				// fall through
			case REASSIGN:
				Template template = (Template) getContext().getSession().createCriteria(Template.class).uniqueResult();

				Properties emailProperties = new Properties();
				ClassLoader loader = EmailManager.class.getClassLoader();
				InputStream istream = loader.getResourceAsStream("Helpline.properties");

				try
				{
					emailProperties.load(istream);
				}
				catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
				String username = emailProperties.getProperty("helpline.reassign");
				
				User user = (User) getContext().getSession()
				.createQuery("from User as user where user.username = ?")
				.setString(0, username)
				.uniqueResult();
				
				EmailManager.forwardEmail(message, template, user);
				getContext().getMessages().add(new LocalizableMessage("forwardEmailMessage"));
				message.setStatus(MessageStatus.REJECTED);
				break;
			case NOREPLY:
				message.setStatus(MessageStatus.NOREPLY);
				break;
			default:
				return getContext().getSourcePageResolution();
			}
			
			message.setQueue(null);

			conv.setOwner(null);
			
			Session session = getContext().getSession();
			session.update(message);
			session.update(conv);
			
			// TODO: discard any draft, cleanup.
		}
		
		catch (MessagingException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return new RedirectResolution("/");
	}
}
